#ifndef SPIPE_H
#define SPIPE_H

#include "pipe.h"

///err_codes:
#define ER_BIND           0xB201
#define ER_LISTEN         0xB202
#define ER_ACCEPT         0xB203
#define ER_EMPTY_QUEE     0xB204

//#define FLAG                     0x0F00
///----------
//#define RB_SIZE                 4096
class SPipe;
typedef fastdelegate::FastDelegate2<SPipe*,i32,void> callback_s;

extern u32 pip_p_initialized;

#define MX_QUEE_E   ENTER_CS(p_mx_quee)
#define MX_QUEE_L   LEAVE_CS(p_mx_quee)


class SPipe
{
public:
    SPipe();
    virtual ~SPipe();

    int PortOpen(u16 port);
    int PortClose();
    int IsReady();
    int Accept(Pipe &pipe);

    callback_s onAccept;
    callback_s onError;


protected:

    int p_socket;
    pthread_t p_thr_accept;
    u16 p_port;
    addrinfo p_hints;
    addrinfo *p_res;
    pthread_mutex_t p_mx_quee;
    int p_exit_flag;
    vector<int> p_quee;
    sockaddr_storage p_remoteaddr;

    static void *th_accept( void * );
    void p_error( int err_code, string err = "");


private:
};

#endif // SPIPE_H
